package coint.btc.controller.btcuser;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.ExpiringSession;
import org.springframework.session.SessionRepository;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import coint.btc.constant.ShenfenEnum;
import coint.btc.dto.BtcUserDto;
import coint.btc.service.btcuser.BtcUserService;
import coint.btc.util.BtcConfig;
import coint.btc.util.DateUtil;
import coint.btc.util.HttpRequest;
import coint.btc.util.HttpStatus;
import coint.btc.util.WriteJson;
import coint.btc.vo.BtcUserVo;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value="用户controller",tags={"用户操作接口"})
@RestController
@RequestMapping("/btcWeb/user")
public class BtcUserController {
	
	private Logger log=LoggerFactory.getLogger(BtcUserController.class);
	
	@Resource(name="sessionRepository")
	private SessionRepository<ExpiringSession> sessionRepository;
	
	@Autowired
	private BtcUserService userService;
    /**
     * 用户登录
     * @param userDto 登录的用户信息
     * @return
     */
	@PostMapping("/login")  
	@ApiOperation(value = "btc前端登录")
	public ModelMap login(HttpServletRequest request,BtcUserDto userDto) {
		if(StringUtils.isBlank(userDto.getParamName())) {
			return WriteJson.errorWebMsg(HttpStatus.NOT_PARAMETER, "昵称/身份证/手机号不能为空");
		}
		if(StringUtils.isBlank(userDto.getMima())) {
			return WriteJson.errorWebMsg(HttpStatus.NOT_PARAMETER, "登录密码不能为空");
		}
		BtcUserVo user=null;
		try {
			user=userService.findUser(userDto);
		} catch (Exception e) {
			log.error("user login is error...",e);
		}
		if(user!=null) {
			HttpSession session = request.getSession();
			session.setAttribute(BtcConfig.loginKey, user);
			log.info("user login is success,{}",userDto.getParamName());
			return WriteJson.successDataMsg("登录成功", user);
		}else {
			request.setAttribute("errorInfo", "验证失败");
			return WriteJson.errorWebMsg(HttpStatus.NOT_LOGIN, "验证失败");
		}
	}
	
   /**
    * 退出登录
    * @param request
    * @return
    */
	@GetMapping("/loginOut")
	@ApiOperation(value = "btc前端登录退出")
	public ModelMap loginOut(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if (session != null) {
			session.setAttribute(session.getId(), null);
		}
		return WriteJson.successMsg("退出登录成功");
	}
	/**
	 * 获取当前用户信息
	 * @param request
	 * @return
	 */
	@GetMapping("/getCurrentUser")
	@ApiOperation(value = "获取当前用户信息")
	public ModelMap getCurrentUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String sessionId = session.getId();//此处session 可以根据传 入参数获取
		ExpiringSession expringSession = sessionRepository.getSession(sessionId);// 根据sessionid 获取 session 同而实现共享
		BtcUserVo user = expringSession.getAttribute(BtcConfig.loginKey);
		return WriteJson.successDataMsg("获取当前用户信息成功", user);
	}
	/**
	 * 注册BTC用户信息
	 * @param userDto BTC用户信息
	 * @return
	 */
	@PostMapping("/registered")
	@ApiOperation(value = "注册BTC用户信息")
	public ModelMap addBtcUser(BtcUserDto userDto,HttpServletRequest request) {
		HttpSession session = request.getSession();
		String sessionId = session.getId();//此处session 可以根据传 入参数获取
		ExpiringSession expringSession = sessionRepository.getSession(sessionId);// 根据sessionid 获取 session 同而实现共享
		BtcUserVo user = expringSession.getAttribute(BtcConfig.loginKey);
		String redom=DateUtil.getReportid();
		String userId="";
		switch(ShenfenEnum.getByValue(user.getShenfen().intValue())) {
		    case CENDUISHANG:
		    	userId=ShenfenEnum.CENDUISHANG.getName()+redom;
		    	break;
		    case SHANGJIA:
		    	userId=ShenfenEnum.SHANGJIA.getName()+redom;
		    	break;
		}
		userDto.setUserid(userId);
		//获取浏览器信息
		Browser browser = UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
		//获取浏览器版本号
	    Version version = browser.getVersion(request.getHeader("User-Agent"));
		String info = browser.getName() + "/" + version.getVersion();
		userDto.getRizhi().setIe(info);
	    /*获取注册服务器ip*/
		userDto.getRizhi().setIp(HttpRequest.getIpAddress(request));
		userDto.getRizhi().setLeixing(1);
		userDto.getRizhi().setShouji(userDto.getShouji());
		userService.addUser(userDto);
		return WriteJson.successMsg("注册成功");
		
	}
	
	
}
